<?php

namespace app\api\model\service;

use think\Model;
use think\Exception;
class Comment extends Model
{
    // 表名
    protected $name = 'service_comment';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 查询评论列表
     * @param $get
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function searchComment($get)
    {
        extract($get);
        $orderBy = 'id desc';
        if(isset($goods_id) && $goods_id != '')
        {
            $where['goods_id'] = $goods_id;
        }
        if(isset($shop_id) && $shop_id != '')
        {
            $where['shop_id'] = $shop_id;
        }
        if(isset($score) && $score != '')
        {
            $where['score'] = $score == 2?['in',[2,3,4]]:$score;
        }
        if(isset($images))
        {
            $where['is_img'] = 1;
        }
        if(isset($skill_id) && $skill_id != '')
        {
            $where['skill_id'] = $skill_id;
        }
        if(isset($user_id) && $user_id != '')
        {
            $where['user_id'] = $user_id;
        }
        $where['state'] = 1;
        $list = self::where($where)
            ->field("id,user_id,score,comment_label_ids,comment_label,content,images,createtime")
            ->order($orderBy)
            ->page($get['page'])
            ->limit(10)
            ->select();
        foreach ($list as &$value)
        {
            $value['user'] = User::where(['id'=>$value['user_id']])->field('id,avatar,nickname')->find();
            $value['images'] = $value['images']?explode(',',$value['images']):'';
        }
        return $list;
    }

    public static function commentAdd($params)
    {
        $order = Order::where(['id'=>$params['order_id'],'status'=>6])->field('id,user_id,skill_id,shop_id,goods_id')->find();
        if(!$order)
        {
            throw new Exception('当前订单无法训练');
        }
        $params['skill_id'] = $order['skill_id'];
        $params['shop_id'] = $order['shop_id'];
        $params['goods_id'] = $order['goods_id'];
        $params['comment_label'] = isset($params['comment_label_ids'])?CommentLabel::getLabel($params['comment_label_ids']):'';
        $params['is_img'] = $params['images']?1:0;
        $comment = new Comment($params);
        $comment->allowField(true)->save();
        Order::where(['id'=>$order['id']])->update(['status'=>7,'updatetime'=>time()]);
        return true;
    }


    /**
     * 统计评论
     * @param $param
     * @return array
     * @throws Exception
     */
    public static function totalComment($params)
    {
        extract($params);
        switch ($params['type'])
        {
            case 'goods':
                $where['goods_id'] = $goods_id;
                break;
            case 'shop':
                $where['shop_id'] = $shop_id;
                break;
            case 'skill':
                $where['skill_id'] = $skill_id;
                break;
            case 'user':
                $where['user_id'] = $user_id;
                break;
        }
        $where['state'] = 1;
        $data['totalNum'] = self::where($where)->count();
        $where['score'] = 5;
        $data['goodNum'] = self::where($where)->count();
        $data['goodPercent'] = $data['totalNum']>0?sprintf("%.2f",$data['goodNum']/$data['totalNum']):1.00;
        $where['score'] = ['in',[2,3,4]];
        $data['middleNum'] = self::where($where)->count();
        $where['score'] = 1;
        $data['badNum'] = self::where($where)->count();
        unset($where['score']);
        $where['is_img'] = 1;
        $data['imageNum'] = self::where($where)->count();
        return $data;
    }

    public static function skillGoodPercent($params)
    {
        $where['skill_id'] = $params['skill_id'];
        $where['state'] = 1;
        $data['totalNum'] = self::where($where)->count();
        $where['score'] = 5;
        $data['goodNum'] = self::where($where)->count();
        return $data['totalNum']>0?bcdiv($data['goodNum'],$data['totalNum'],2):1.00;;
    }



    public static function getCommentScore($params)
    {
        $params['state'] = 1;
        $total = self::where($params)->count();
        $score = $total>0?sprintf("%.2f",self::where($params)->avg('score')):5.0;
        return $score;
    }


    public static function getCount($where)
    {
        return self::where($where)->count('id');
    }
}